package com.lu.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Properties;

public class ALYMailUtils {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final String ALIDM_SMTP_HOST = "smtpdm.aliyun.com";
    private static final String ALIDM_SMTP_PORT = "80";//或"80"

    /**
     * 邮件发送器
     *
     * @return 配置好的工具
     */
    private static JavaMailSenderImpl createMailSender(String username,String password) {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
//        sender.setHost(ALIDM_SMTP_HOST);
        //阿里服务器禁用25端口，故在配置文件里配置了ssl+465端口的服务
//        sender.setPort(ALIDM_SMTP_PORT);
        sender.setUsername(username);
        sender.setPassword(password);
        sender.setDefaultEncoding("Utf-8");
        Properties p = new Properties();
        p.put("mail.smtp.auth", "true");
        p.put("mail.smtp.host", ALIDM_SMTP_HOST);
        p.put("mail.smtp.port", ALIDM_SMTP_PORT);
        // 如果使用ssl，则去掉使用25端口的配置，进行如下配置,
        // props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        // props.put("mail.smtp.socketFactory.port", "465");
        // props.put("mail.smtp.port", "465");
        //发件人的账号
        p.put("mail.user", "showlu@mail.showlu.top");
        // 访问SMTP服务时需要提供的密码
        p.put("mail.password", "ShowLu19970108x");
        sender.setJavaMailProperties(p);
        return sender;
    }

    public void sendSimpleEmail(String from, String fromPassword, String to, String subject, String content) {
        JavaMailSenderImpl mailSender = createMailSender(from,fromPassword);
        SimpleMailMessage message = new SimpleMailMessage();//创建简单邮件消息
        message.setFrom(from);//设置发送人
        message.setTo(to);//设置收件人

        /* String[] adds = {"xxx@qq.com","yyy@qq.com"}; //同时发送给多人
        message.setTo(adds);*/

        message.setSubject(subject);//设置主题
        message.setText(content);//设置内容
        try {
            mailSender.send(message);//执行发送邮件
            logger.info("简单邮件已经发送。");
        } catch (Exception e) {
            logger.error("发送简单邮件时发生异常！", e);
        }
    }

    public void sendHtmlEmail(String from, String fromPassword, String to, String subject, String content) {
        JavaMailSenderImpl mailSender = createMailSender(from,fromPassword);
        MimeMessage message = mailSender.createMimeMessage();//创建一个MINE消息

        try {
            //true表示需要创建一个multipart message
            MimeMessageHelper helper = new MimeMessageHelper(message, true,"UTF-8");
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);

            mailSender.send(message);
            logger.info("html邮件发送成功");
        } catch (MessagingException e) {
            logger.error("发送html邮件时发生异常！", e);
        }

    }

    public void sendAttachmentsEmail(String from, String fromPassword, String to, String subject, String content, String filePath) {
        JavaMailSenderImpl mailSender = createMailSender(from,fromPassword);
        MimeMessage message = mailSender.createMimeMessage();//创建一个MINE消息

        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);// true表示这个邮件是有附件的

            FileSystemResource file = new FileSystemResource(new File(filePath));//创建文件系统资源
            String fileName = filePath.substring(filePath.lastIndexOf(File.separator)+1,filePath.length());
            helper.addAttachment(fileName, file);//添加附件
            mailSender.send(message);
            logger.info("带附件的邮件已经发送。");
        } catch (MessagingException e) {
            logger.error("发送带附件的邮件时发生异常！", e);
        }
    }

    public void sendInlineResourceEmail(String from, String fromPassword, String to, String subject, String content, String rscPath, String rscId) {
        JavaMailSenderImpl mailSender = createMailSender(from,fromPassword);
        MimeMessage message = mailSender.createMimeMessage();

        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);

            FileSystemResource res = new FileSystemResource(new File(rscPath));

            //添加内联资源，一个id对应一个资源，最终通过id来找到该资源
            helper.addInline(rscId, res);//添加多个图片可以使用多条 <img src='cid:" + rscId + "' > 和 helper.addInline(rscId, res) 来实现

            mailSender.send(message);
            logger.info("嵌入静态资源的邮件已经发送。");
        } catch (MessagingException e) {
            logger.error("发送嵌入静态资源的邮件时发生异常！", e);
        }

    }
    public static void main(String[] args) {
        //貌似图片不可以
            new ALYMailUtils().sendAttachmentsEmail("showlu@mail.showlu.top","ShowLu19970108x",
                    "1309617271@qq.com","测试三","文档","C:\\Users\\Administrator\\Pictures\\People\\1.jpg");
    }
}
